package com.wenchao.quickstart.utils;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Random;

/**
 * 设备标识工具
 */
public class UUIDUtil {

    private static String getTelephonySerial(Context context) {
    	try {
        	TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE );
        	return TelephonyMgr.getDeviceId() + TelephonyMgr.getSimSerialNumber() + TelephonyMgr.getSubscriberId();
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    		return "unknown";
    	}
    }
    
    @SuppressWarnings("deprecation")
	private static String getBuildInfo() {
    	try {
        	return Build.BRAND + Build.CPU_ABI + Build.DEVICE + Build.DISPLAY + Build.HOST + Build.ID + Build.MANUFACTURER + Build.MODEL + Build.PRODUCT + Build.TAGS + Build.TYPE + Build.USER;
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    		return "unknown";
    	}	
    }
    
    private static String getAndroidID(Context context) {
    	try {
        	return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    		return "unknown";
    	}    }
    
    private static String getSystemSerial() {
    	Class<?> c;
		try {
			c = Class.forName("android.os.SystemProperties");
	    	 Method get = c.getMethod("get", String.class, String.class );
	    	 return (String)(get.invoke(c,"ro.serialno", "unknown"));
		} catch (Exception e) {
			return "unknown";
		}
    }
    
    private static String getWifiMAC(Context context) {
    	try {
    		WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    		return wm.getConnectionInfo().getMacAddress();
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    		return "unknown";
    	}
    }
    public static String getDeviceModel() {
		if (Build.MANUFACTURER == null && Build.MODEL == null) return "unknown";
		else if (Build.MANUFACTURER == null) return "#" + Build.MODEL;
		else if (Build.MODEL == null) return Build.MANUFACTURER + "#";
		else return Build.MANUFACTURER + "#" + Build.MODEL;
    }
    
    public static String getDeviceUUID(Context context) {
    	//System.out.println("1 "+ Build.BRAND + " 2 " + Build.CPU_ABI + " 3 " + Build.DEVICE + " 4 " + Build.DISPLAY + " 5 " + Build.HOST + " 6 " + Build.ID + " 7 " + Build.MANUFACTURER + " 8 " + Build.MODEL + " 9 " + Build.PRODUCT + " a " + Build.TAGS + " b " + Build.TYPE + " c "  + Build.USER);
    	//System.out.println("T=" + getTelephonySerial(context));
    	//System.out.println("W="  + getWifiMAC(context));
    	//System.out.println("I="  + getAndroidID(context));
    	//System.out.println("S="  + getSystemSerial());
    	return MD5(getTelephonySerial(context) + getBuildInfo() + getWifiMAC(context) + getAndroidID(context) + getSystemSerial());
    }
    
    public static String getDeviceUUIDAndRandom(Context context) {
    	Random random = new Random(System.currentTimeMillis());
    	return MD5(random.nextInt() + getDeviceUUID(context) + System.currentTimeMillis());
    }

    public static String MD5(String Source) {
        MessageDigest mdEnc = null;
        try {
            mdEnc = MessageDigest.getInstance("MD5");
            mdEnc.update(Source.getBytes(), 0, Source.length());
            return new BigInteger(1, mdEnc.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        	return null;
        }
    }
    
}
